package com.studentmanage.manage_college.service;

import com.studentmanage.framework.domain.base.College;
import com.studentmanage.framework.domain.base.response.CollegeCode;
import com.studentmanage.framework.domain.common.response.CommonCode;
import com.studentmanage.framework.domain.common.response.ResponseResult;
import com.studentmanage.framework.domain.common.response.ResponseValue;
import com.studentmanage.manage_college.dao.CollegeRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Optional;

@Service
public class CollegeService {
    @Autowired
    private CollegeRepository collegeRepository;

    //添加学院信息
    public ResponseResult addCollege(College college){
        if(StringUtils.isEmpty(college.getName())){
            return new ResponseResult(CollegeCode.ISNULL);
        }
        College college1 = collegeRepository.findByName(college.getName());
        if(college1 != null){
            return new ResponseResult(CollegeCode.ADD_FAIL);
        }
        collegeRepository.save(college);
        return new ResponseResult(CommonCode.SUCCESS);
    }

    //根据id查询学院
    public ResponseValue<College> findCollegeById(String id) {
        Optional<College> optional = collegeRepository.findById(id);
        if(optional.isPresent()){
            College college = optional.get();
            return new ResponseValue<College>(true, college);
        }
        return new ResponseValue<College>(false, null);
    }
}
